package com.ruoyi.system.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.imp.SpecImport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SpecMapper;
import com.ruoyi.system.domain.Spec;
import com.ruoyi.system.service.ISpecService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 竞品规格Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-30
 */
@Service
public class SpecServiceImpl implements ISpecService {
    @Resource
    private SpecMapper specMapper;

    /**
     * 查询竞品规格
     *
     * @param id 竞品规格主键
     * @return 竞品规格
     */
    @Override
    public Spec selectSpecById(Long id) {
        return specMapper.selectSpecById(id);
    }

    /**
     * 查询竞品规格列表
     *
     * @param spec 竞品规格
     * @return 竞品规格
     */
    @Override
    public List<Spec> selectSpecList(Spec spec) {
        return specMapper.selectSpecList(spec);
    }

    /**
     * 新增竞品规格
     *
     * @param spec 竞品规格
     * @return 结果
     */
    @Override
    public int insertSpec(Spec spec) {
        spec.setCreateTime(DateUtils.getNowDate());
        List<Spec> specs = specMapper.selectSpecList(new Spec() {{
            setName(spec.getName());
        }});
        if (specs != null && !specs.isEmpty()) {
            throw new ServiceException("规格已存在");
        }
        return specMapper.insertSpec(spec);
    }

    /**
     * 修改竞品规格
     *
     * @param spec 竞品规格
     * @return 结果
     */
    @Override
    public int updateSpec(Spec spec) {
        spec.setUpdateTime(DateUtils.getNowDate());
        List<Spec> specs = specMapper.selectSpecList(new Spec() {{
            setName(spec.getName());
        }});
        if (specs != null && !specs.isEmpty()) {
            if (!specs.get(0).getId().equals(spec.getId())) {
                throw new ServiceException("规格已存在");
            }
        }
        return specMapper.updateSpec(spec);
    }

    /**
     * 批量删除竞品规格
     *
     * @param ids 需要删除的竞品规格主键
     * @return 结果
     */
    @Override
    public int deleteSpecByIds(Long[] ids) {
        return specMapper.deleteSpecByIds(ids);
    }

    /**
     * 删除竞品规格信息
     *
     * @param id 竞品规格主键
     * @return 结果
     */
    @Override
    public int deleteSpecById(Long id) {
        return specMapper.deleteSpecById(id);
    }

    /**
     * 导入规格
     */
    @Override
    @Transactional
    public void importSpec(List<SpecImport> list) {
        Map<String, Spec> specMap = specMapper.selectSpecList(new Spec()).stream().collect(Collectors.toMap(Spec::getName, t -> t));
        List<SpecImport> errorList = new ArrayList<>(10);
        List<Spec> specs = new ArrayList<>(10);
        Date date = new Date();
        for (SpecImport item : list) {
            if (specMap.containsKey(item.getName())) {
                errorList.add(item);
            } else {
                Spec spec = new Spec(item.getName());
                spec.setCreateTime(date);
                specs.add(spec);
            }
        }
        if (!errorList.isEmpty()) {
            StringBuilder errMsg = new StringBuilder();
            errorList.forEach(item -> {
                errMsg.append(item.getName()).append("已存在\n");
            });
            throw new ServiceException(errMsg.toString());
        }
        if (!specs.isEmpty()) {
            specMapper.insertSpecBatch(new HashMap<String, Object>() {{
                put("specList", specs);
            }});
        }
    }
}
